package com.springcloudalibaba.sellerProduct.controller;

import com.springcloudalibaba.bclass.base.BaseResult;
import com.springcloudalibaba.sellerProduct.entity.SellerProductEntity;
import com.springcloudalibaba.sellerProduct.service.SellerProductService;
import io.seata.core.context.RootContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 卖家发布商品信息 前端控制器
 * </p>
 *
 * @author lixing_java@163.com
 * @since 2021-11-27
 */
@RestController
@RequestMapping("/product")
public class SellerProductController {
    @Resource
    private SellerProductService sellerProductService;

    /**
     * @描述 修改商品库存
     * @作者 lixing
     * @日期 2021/6/17 8:50
     * @Param [SellerProductVo: id 库存编号，stock 库存变更数量]
     * @return com.eureka.bclass.base.BaseResult<java.lang.String>
     */
    @PostMapping("changeStock")
    public BaseResult<String> stock(@RequestBody SellerProductEntity po) {
        System.out.println("xid========================" + RootContext.getXID());
        Integer stock = po.getStock();
        po = sellerProductService.getById(po.getId());
        if (null == po) {
            return new BaseResult<String>().fail("商品不存在");
        } else {
            if (po.getStock() <= 0) {
                return new BaseResult<String>().fail("商品已售罄");
            }
        }
        po.setStock(po.getStock() - stock);
        boolean flag = sellerProductService.updateById(po);
        return flag ? new BaseResult<String>().success("扣减成功") : new BaseResult<String>().fail("扣减失败");
    }
}

